using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Common;
using RMAS.DAL;

namespace RMAS.DAL
{
    /// <summary>
    /// Summary description for TOURGUIDE.
     /// </summary>
	public class TOURGUIDEDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public TOURGUIDEDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public TOURGUIDEDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT ID, Picture, PathPiture, Clip, PathClip, ISClipTour FROM TOURGUIDE WHERE (ID = @ID)";
	        _SQLSelectByKey = query;

            query = @"UPDATE TOURGUIDE SET Picture = @Picture, PathPiture = @PathPiture, Clip = @Clip, PathClip = @PathClip, ISClipTour = @ISClipTour WHERE (ID = @ID)";
	        _SQLUpdate = query;

            query = @"INSERT INTO TOURGUIDE VALUES (@ID, @Picture, @PathPiture, @Clip, @PathClip, @ISClipTour)";
	        _SQLInsert = query;

            query = @"DELETE FROM TOURGUIDE WHERE (ID = @ID)";
	        _SQLDelete = query;

            query = @"SELECT ID, Picture, PathPiture, Clip, PathClip, ISClipTour FROM TOURGUIDE";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(TOURGUIDE obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.Int32);
                         DalTools.addDbDataParameter(command, "Picture", obj.Picture, DbType.String);
         DalTools.addDbDataParameter(command, "PathPiture", obj.PathPiture, DbType.String);
         DalTools.addDbDataParameter(command, "Clip", obj.Clip, DbType.String);
         DalTools.addDbDataParameter(command, "PathClip", obj.PathClip, DbType.String);
         DalTools.addDbDataParameter(command, "ISClipTour", obj.ISClipTour, DbType.Boolean);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(TOURGUIDE obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.Int32);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(TOURGUIDE obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(TOURGUIDE obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private TOURGUIDEList SelectListByCommand(IDbCommand command)
        {
            TOURGUIDEList list = new TOURGUIDEList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
		TOURGUIDE obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new TOURGUIDE();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private TOURGUIDEList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            TOURGUIDEList list = new TOURGUIDEList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				TOURGUIDE obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new TOURGUIDE();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(TOURGUIDE obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["ID"] is DBNull))
		{
			obj.ID = (Int32)reader["ID"];
		}
                if(!(reader["Picture"] is DBNull))
		{
			obj.Picture = (string)reader["Picture"];
		}
                if(!(reader["PathPiture"] is DBNull))
		{
			obj.PathPiture = (string)reader["PathPiture"];
		}
                if(!(reader["Clip"] is DBNull))
		{
			obj.Clip = (string)reader["Clip"];
		}
                if(!(reader["PathClip"] is DBNull))
		{
			obj.PathClip = (string)reader["PathClip"];
		}
                if(!(reader["ISClipTour"] is DBNull))
		{
			obj.ISClipTour = (bool)reader["ISClipTour"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public TOURGUIDE SelectByKey(Int32 ID)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "ID", ID, DbType.Int32);
      	    TOURGUIDEList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public TOURGUIDE SelectByKey(Int32 ID, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "ID", ID, DbType.Int32);
      	    TOURGUIDEList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public TOURGUIDEList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    TOURGUIDEList list =  SelectListByCommand(command);
	        return list;
        }
	public TOURGUIDEList SelectAll(IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    TOURGUIDEList list =  SelectListByCommand(command, trans);
	        return list;
        }
         public TOURGUIDEList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    TOURGUIDEList list =  SelectListByCommand(command);
	        return list;
        }

        public TOURGUIDEList SelectByCondition(TOURGUIDE obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM TOURGUIDE " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            TOURGUIDEList list = SelectListByCommand(command, trans);
            return list;
        }
		
		 public TOURGUIDEList SelectByCondition(TOURGUIDE obj)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM TOURGUIDE " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            TOURGUIDEList list = SelectListByCommand(command);
            return list;
        }
        private void DeleteByCondition(TOURGUIDE obj, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM TOURGUIDE " + GetCondtionQuery(obj) + ";";
            FillParamToCommand(command, obj);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(TOURGUIDE obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetID)
	{
    	    s += " AND ID = @ID ";
	}
	if (obj.IsSetPicture)
	{
    	    s += " AND Picture = @Picture ";
	}
	if (obj.IsSetPathPiture)
	{
    	    s += " AND PathPiture = @PathPiture ";
	}
	if (obj.IsSetClip)
	{
    	    s += " AND Clip = @Clip ";
	}
	if (obj.IsSetPathClip)
	{
    	    s += " AND PathClip = @PathClip ";
	}
	if (obj.IsSetISClipTour)
	{
    	    s += " AND ISClipTour = @ISClipTour ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, TOURGUIDE obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetID)
            {
            	DalTools.addDbDataParameter(command, "ID", obj.ID, DbType.Int32); 
            }
            if (obj.IsSetPicture)
            {
            	DalTools.addDbDataParameter(command, "Picture", obj.Picture, DbType.String); 
            }
            if (obj.IsSetPathPiture)
            {
            	DalTools.addDbDataParameter(command, "PathPiture", obj.PathPiture, DbType.String); 
            }
            if (obj.IsSetClip)
            {
            	DalTools.addDbDataParameter(command, "Clip", obj.Clip, DbType.String); 
            }
            if (obj.IsSetPathClip)
            {
            	DalTools.addDbDataParameter(command, "PathClip", obj.PathClip, DbType.String); 
            }
            if (obj.IsSetISClipTour)
            {
            	DalTools.addDbDataParameter(command, "ISClipTour", obj.ISClipTour, DbType.Boolean); 
            }
        }
        #endregion
    }
}